// import {
//   request
// } from "../../request/index";

// // pages/goods_detail/goods_detail.js
// Page({
//   data: {
//     detaillist: [],
//     isCollect: false,
//     detailindex: [],
//   },
//   onLoad: function (options) {
//     let {
//       goods_id
//     } = options

//     this.getdetail(goods_id)
//   },

//   getdetail(goods_id) {
//     request({
//       url: '/goods/detail',
//       data: {
//         goods_id
//       }
//     }).then(res => {
//       console.log(res);
//       this.setData({
//         detaillist: res
//       })
//     })
//   },

//   // 点击加入购物车
//   handleCartAdd() {
//     let cart = wx.getStorageSync("cart") || [];
//     let index = cart.findIndex(v => v.goods_id === this.GoodsInfo.goods_id);
//     if (index === -1) {
//       this.GoodsInfo.num = 1;
//       this.GoodsInfo.checked = true;
//       cart.push(this.GoodsInfo)
//     } else {
//       cart[index].num++;
//     }
//     wx.setStorageSync("cart", cart);
//     wx.showToast({
//       title: '加入成功',
//       mask: true
//     });
//   },

//   handleCollect() {

//   }
// })
/*
 * @Author: your name
 * @Date: 2021-09-04 08:54:00
 * @LastEditTime: 2021-09-09 16:20:28
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \maidoubuy\pages\goods_detail\goods_detail.js
 */
import {
  request
} from "../../request/index"
Page({
  /**
   * 1:请求数据
   * 2:点击轮播图，预览大图
   *   1:给轮播图一个点击事件
   *   2:调用小程序api  previewImage
   * 3:点击加入购物车
   *   1:先绑定点击事件
   *   2:获取缓存中购物车数据
   *   3:先判断  当前的商品是不是已经在购物车里面了
   *   4：已经存在，修改商品数据，数量++，重新再把购物车的数据数组存入到缓存里面
   *   5：不存在---push---带上购买的数量；---再把购物车的数据数组存入到缓存里面
   *   6:弹出提示
   * 4:商品收藏
   *   1:页面onshow的时候，加载缓存中的商品收藏的数据
   *   2:判断当前商品是不是被收藏
   *      1:是 ---改变图标
   *   3:点击收藏按钮
   *    1:判断这个商品是不是存在于缓存数组中
   *    2:已经存在，吧这个商品删除
   *    3：没有存在---吧这个商品添加到收藏数组里面
   */
  data: {
    goodsObj: {},
    //商品是否被收藏
    isCollect: false
  },
  // 商品对象;
  GoodsInfo: {},
  onShow: function () {
    //返回的是一个数组，第一项是首页，最后一项是当前页面;
    let pages = getCurrentPages();
    let currentPage = pages[pages.length - 1];
    let options = currentPage.options;
    const {
      goods_id
    } = options;
    this.getGoodsDetail(goods_id);
  },
  async getGoodsDetail(goods_id) {
    const goodsObj = await request({
      url: '/goods/detail',
      data: {
        goods_id
      }
    });
    this.GoodsInfo = goodsObj;
    console.log(this.GoodsInfo)
    //1:获取缓存中页面收藏的商品;
    let collect = wx.getStorageSync("collect") || [];
    //2:判断当前商品是不是被收藏
    let isCollect = collect.some(v => {
      return v.goods_id === this.GoodsInfo.goods_id
    })
    //png jpg gif webp
    //webp -----部分型号的苹果手机不识别webp图片
    //最好是找后台修改;
    //临时自己改----.webp ----.jpg
    this.setData({
      goodsObj: {
        goods_name: goodsObj.goods_name,
        goods_price: goodsObj.goods_price,
        goods_introduce: goodsObj.goods_introduce.replace(/\.webp/g, '.jpg'),
        pics: goodsObj.pics
      },
      isCollect
    })
  },
  // 预览效果
  handlePreviewImage(e) {
    const urls = this.GoodsInfo.pics.map(v => v.pics_mid);
    //接收传递过来的url;
    const current = e.currentTarget.dataset.url;
    wx.previewImage({
      current,
      urls
    });
  },
  // 点击加入购物车
  handleCartAdd() {
    let cart = wx.getStorageSync("cart") || [];
    let index = cart.findIndex(v => v.goods_id === this.GoodsInfo.goods_id);
    if (index === -1) {
      this.GoodsInfo.num = 1;
      this.GoodsInfo.checked = true;
      cart.push(this.GoodsInfo)
    } else {
      cart[index].num++;
    }
    wx.setStorageSync("cart", cart);
    wx.showToast({
      title: '加入成功',
      mask: true
    });
  },
  handleCollect() {
    let isCollect = false;
    let collect = wx.getStorageSync("collect") || [];
    let index = collect.findIndex(v => v.goods_id === this.GoodsInfo.goods_id);
    if (index !== -1) {
      //已经收藏过了;   在数组中去删除这个收藏信息;
      collect.splice(index, 1);
      isCollect = false;
      wx.showToast({
        title: '取消成功',
        icon: 'success',
        mask: true
      })
    } else {
      collect.push(this.GoodsInfo);
      isCollect = true;
      wx.showToast({
        title: '收藏成功',
        icon: 'success',
        mask: true
      });
    }

    wx.setStorageSync('collect', collect);

    this.setData({
      isCollect
    })

  }
})